import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_pad_widgets/resources/kq_pad_theme_colors.dart';
import 'package:kq_flutter_pad_widgets/widgets/divider/kq_pad_divider.dart';

import '../widgets/form/entity/kq_form_entity.dart';
import '../widgets/form/kq_edit_item_view.dart';
import '../widgets/form/kq_radio_item_view.dart';

/// 表单控件辅助工具类
class KqPadFormUtil {
  /// 渲染多个表单组件
  static List<Widget> renderFormWidgetList(List<KqPadFormEntity> entities) {
    if (entities.isEmpty) {
      return [];
    }
    return entities.map((e) => renderFormWidget(e)).toList();
  }

  /// 渲染多个表单组件，使用[Column]包起来，并自动补全顶部的border
  static Widget renderFormWidgetListWithBorder(List<KqPadFormEntity> entities) {
    if (entities.isEmpty) {
      return Container();
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        const KqPadDivider(
          color: KqPadThemeColors.bgF0,
        ),
        ...entities.map((e) => renderFormWidget(e)).toList()
      ],
    );
  }

  /// 渲染单个表单组件
  static Widget renderFormWidget(KqPadFormEntity entity) {
    if (entity.itemType != null) {
      switch (entity.itemType!) {
        case ItemType.edit:
          return KqPadEditItemView(entity: entity);
        case ItemType.radio:
          return KqPadRadioItemView(entity: entity);
      }
    }
    return Container();
  }

  /// 根据ItemId 查找KqPadFormEntity
  static KqPadFormEntity? findEntityByItemId(
      List<KqPadFormEntity> data, String itemId) {
    for (var value in data) {
      if (value.itemId == itemId) {
        return value;
      }
    }
    return null;
  }

  /// 根据contentKey或contentKeyForShow 查找KqPadFormEntity
  static KqPadFormEntity? findEntityByContentKeyOrContentKeyForShow(
      List<KqPadFormEntity> list,
      {String? contentKey,
      String? contentKeyForShow}) {
    KqPadFormEntity? entity;
    for (int i = 0; i < list.length; i++) {
      KqPadFormEntity item = list[i];
      if (contentKey != null) {
        if (contentKeyForShow != null) {
          if (item.contentKey == contentKey &&
              item.contentKeyForShow == contentKeyForShow) {
            entity = item;
            break;
          }
        } else {
          if (item.contentKey == contentKey) {
            entity = item;
            break;
          }
        }
      } else {
        if (contentKeyForShow != null) {
          if (item.contentKeyForShow == contentKeyForShow) {
            entity = item;
            break;
          }
        }
      }
    }
    return entity;
  }

  /// 查找List<KqPadFormEntity>中所有的itemIds对应的实体
  static List<KqPadFormEntity> findAllEntityByItemIds(
      List<KqPadFormEntity> data, List<String> itemIds) {
    List<KqPadFormEntity> list = [];
    for (var value in data) {
      for (var itemId in itemIds) {
        if (value.itemId == itemId) {
          list.add(value);
        }
      }
    }
    return list;
  }

  /// 根据标题 查找KqPadFormEntity
  static KqPadFormEntity? findEntityByTitle(
      List<KqPadFormEntity> data, String title) {
    for (var value in data) {
      if (value.title == title) {
        return value;
      }
    }
    return null;
  }

  /// 快捷设置实体类数组中itemId对应实体类的值
  static setValueForEntityByItemId({
    required List<KqPadFormEntity> list,
    required String itemId,
    String? valueForShow,
    String? valueForSubmit,
  }) {
    var item = findEntityByItemId(list, itemId);
    if (item != null) {
      item.valueForSubmit = valueForSubmit;
      item.valueForShow = valueForShow;
      item.updateWidget();
    }
  }

  /// 快捷设置实体类数组中itemId对应实体类的是否可编辑
  static setEditableByItemId({
    required List<KqPadFormEntity> list,
    required String itemId,
    bool? editable,
    bool? editableMask,
  }) {
    var item = findEntityByItemId(list, itemId);
    if (item != null) {
      if (editable != null) {
        item.editable = editable;
      }
      if (editableMask != null) {
        item.editableMask = editableMask;
      }
      item.updateWidget();
    }
  }

  /// 找到数组里[itemId]对应的Entity，并清空表单的数据
  static clearFormValueByItemId(List<KqPadFormEntity> list, String itemId) {
    var entity2 = KqPadFormUtil.findEntityByItemId(list, itemId);
    if (entity2 != null) {
      entity2.valueForShow = null;
      entity2.valueForSubmit = null;
      entity2.qty = entity2.minQty;
      entity2.updateWidget();
    }
  }

  /// 快捷清空实体类数组中itemId对应实体类的值，包括valueForShow，valueForSubmit
  static clearFormValueByItemIds(
      {required List<KqPadFormEntity> list, required List<String> itemIds}) {
    var items = findAllEntityByItemIds(list, itemIds);
    if (items.isNotEmpty) {
      for (var item in items) {
        item.valueForSubmit = null;
        item.valueForShow = null;
        item.qty = item.minQty;
        item.updateWidget();
      }
    }
  }

  /// 生成提交给后端的数据
  static Map<String, dynamic> getFormSubmitJson(List<KqPadFormEntity> data) {
    Map<String, dynamic> params = {};
    for (var entity in data) {
      if (entity.itemType != null) {
        switch (entity.itemType!) {
          case ItemType.edit:
          case ItemType.radio:
            if (entity.contentKey != null) {
              params[entity.contentKey!] = entity.valueForSubmit;
            }
            if (entity.contentKeyForShow != null) {
              params[entity.contentKeyForShow!] = entity.valueForShow;
            }
            break;
        }
        // 提交单位
        if (entity.contentKeyForUnit != null) {
          params[entity.contentKeyForUnit!] = entity.unit;
        }
      }
    }
    return params;
  }
}
